<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="需求标题" prop="requirementTitle">
        <el-input
          v-model="queryParams.requirementTitle"
          placeholder="请输入需求标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['demand:needs:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['demand:needs:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['demand:needs:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-document"
          size="mini"
          :disabled="single"
          @click="handleReply"
          v-hasPermi="['demand:needs:reply']"
          >回复</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="demandList"
      @selection-change="handleSelectionChange"
      height="665px"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column
        label="需求标题"
        align="center"
        prop="requirementTitle"
        width="150px"
        fixed
      />
      <el-table-column
        label="需求描述"
        align="center"
        prop="description"
        width="500px"
        fixed
      >
        <template slot-scope="scope">
          <editor
            v-model="scope.row.description"
            :min-height="200"
            ref="quillEditor"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="需求图片"
        align="center"
        prop="demandImg"
        width="100px"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.demandImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="提交人/提交日期"
        align="center"
        prop="author"
        width="135px"
      >
        <template slot-scope="scope">
          {{ scope.row.author }}<br />{{
            parseTime(scope.row.addTime, "{y}-{m}-{d}")
          }}
          {{ parseTime(scope.row.addTime, "{h}:{i}") }}
        </template>
      </el-table-column>
      <el-table-column
        label="回复需求"
        align="center"
        prop="reply"
        width="500px"
      >
        <template slot-scope="scope">
          <editor
            v-model="scope.row.reply"
            :min-height="200"
            ref="quillEditor"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="流程图"
        align="center"
        prop="replyImg"
        width="100px"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.replyImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="回复状态" align="center" prop="replyStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reply_status"
            :value="scope.row.replyStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.audit_status"
            :value="scope.row.auditStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="需求状态" align="center" prop="demandStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.demand_status"
            :value="scope.row.demandStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['demand:needs:edit', 'demand:needs:remove']"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right"
              >更多</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleUpdate"
                icon="el-icon-edit"
                v-hasPermi="['demand:needs:edit']"
                >修改</el-dropdown-item
              >
              <el-dropdown-item
                command="handleDelete"
                icon="el-icon-delete"
                v-hasPermi="['demand:needs:remove']"
                >删除</el-dropdown-item
              >
              <el-dropdown-item
                command="handleAudit"
                icon="el-icon-circle-check"
                v-hasPermi="['demand:needs:audit']"
                >审核</el-dropdown-item
              >
              <el-dropdown-item
                command="handleRefuse"
                icon="el-icon-circle-close"
                v-hasPermi="['demand:needs:audit']"
                >反审核</el-dropdown-item
              >
              <el-dropdown-item
                command="handlefinish"
                icon="el-icon-check"
                v-hasPermi="['demand:needs:finish']"
                >完成</el-dropdown-item
              >
              <el-dropdown-item
                command="handleCancel"
                icon="el-icon-check"
                v-hasPermi="['demand:needs:finish']"
                >取消</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售中心对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="需求标题" prop="requirementTitle">
              <el-input
                v-model="form.requirementTitle"
                placeholder="请输入需求标题"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <editor
              v-model="form.description"
              :min-height="200"
              ref="quillEditor"
            />
          </el-col>
        </el-row>
        <br />
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求图片" prop="demandImg">
              <image-upload v-model="form.demandImg" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="titleReply"
      :visible.sync="openReply"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="formReply"
        :model="formReply"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="回复需求" prop="reply">
              <editor
                v-model="formReply.reply"
                :min-height="200"
                ref="quillEditor"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求图片" prop="replyImg">
              <image-upload v-model="formReply.replyImg" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormReply">确 定</el-button>
        <el-button @click="cancelReply">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDemand,
  addDemand,
  getDemand,
  updateDemand,
  delDemand,
  replyDemand,
  audit,
  finish,
} from "@/api/demand/needs";
export default {
  dicts: ["demand_status", "reply_status", "audit_status"],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      total: 0,
      form: {},
      formReply: {},
      ids: [],
      // 遮罩层
      loading: false,
      title: "",
      titleReply: "",
      open: false,
      openReply: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requirementTitle: null,
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      demandList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        requirementTitle: null,
        description: null,
        demandImg: null,
      };
      this.resetForm("form");
    },
    getList() {
      listDemand(this.queryParams).then((response) => {
        console.log(response);
        this.total = response.total;
        this.demandList = response.rows;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //新增
    handleAdd() {
      this.reset();
      this.title = "添加需求";
      this.open = true;
    },

    //取消
    cancel() {
      this.open = false;
      this.reset();
    },
    //确定
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.demandId == null) {
            addDemand(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateDemand(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.demandId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改 */
    handleUpdate(row) {
      this.title = "修改需求";
      this.open = true;
      this.reset();
      const id = row.demandId || this.ids;
      getDemand(id).then((response) => {
        this.form = response.data;
      });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdate":
          this.handleUpdate(row);
          break;
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleAudit":
          this.handleAudit(row);
          break;
        case "handleRefuse":
          this.handleRefuse(row);
          break;
        case "handlefinish":
          this.handlefinish(row);
          break;
        case "handleCancel":
          this.handleCancel(row);
          break;
        default:
          break;
      }
    },
    /** 删除 */
    handleDelete(row) {
      const id = row.demandId || this.ids;
      this.$modal
        .confirm('是否确认删除需求编号为"' + id + '"的数据项?')
        .then(function () {
          return delDemand(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 回复 */
    handleReply(row) {
      this.titleReply = "回复需求";
      this.openReply = true;
      const id = row.demandId || this.ids;
      this.formReply = { demandId: id[0] };
      getDemand(id).then((response) => {
        this.formReply = response.data;
      });
    },
    /** 回复确定按钮 */
    submitFormReply() {
      this.$refs["formReply"].validate((valid) => {
        if (valid) {
          if (this.formReply.demandId != null) {
            replyDemand(this.formReply).then((response) => {
              this.$modal.msgSuccess("回复成功");
              this.openReply = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 回复取消按钮 */
    cancelReply() {
      this.openReply = false;
      this.reset();
    },
    /** 同意审核 */
    handleAudit(row) {
      this.form = row;
      this.form.flay = true;
      audit(this.form).then((response) => {
        this.$modal.msgSuccess("审核成功");
        this.getList();
      });
    },
    /** 反审核 */
    handleRefuse(row) {
      this.form = row;
      this.form.flay = false;
      audit(this.form).then((response) => {
        this.$modal.msgSuccess("反审核成功");
        this.getList();
      });
    },
    /** 完成 */
    handlefinish(row) {
      this.form = row;
      this.form.flay = true;
      finish(this.form).then((response) => {
        this.$modal.msgSuccess("完成成功");
        this.getList();
      });
    },
    /** 取消 */
    handleCancel(row) {
      this.form = row;
      this.form.flay = false;
      finish(this.form).then((response) => {
        this.$modal.msgSuccess("取消成功");
        this.getList();
      });
    },
  },
};
</script>

<style>
</style>
